/**
 * 
 */
package guice.trails.cardeal;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Inject;
import com.google.inject.Injector;

/**
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @version $Id$
 */
public class CarDealer {

	@Inject
	private Injector injector;

	public <T extends Car> T request(Class<T> car) throws OutOfStockException {
		T requestedCar = injector.getInstance(car);

		if (null == requestedCar)
			throw new OutOfStockException(String.format("Requested %s car is out of stock. Please try again later.", car
					.getSimpleName()));

		return requestedCar;
	}

	public <T extends Car> List<T> request(Class<T> car, int qty) throws OutOfStockException {
		List<T> bulk = new ArrayList<T>();

		for (int i = 0; i < qty; i++) {
			T requestedCar = injector.getInstance(car);
			if (null == requestedCar)
				throw new OutOfStockException(String.format("Order of %s %s cars exceed our stocks. Please try again later.",
						qty, car.getSimpleName()));

			bulk.add(requestedCar);
		}

		return bulk;
	}

}
